/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#ifndef BACKENDSQLITE_H
#define BACKENDSQLITE_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>

#include <backendinterface.h>

class BackendSQLite : public QObject, public BackendInterface
{
    Q_OBJECT
    Q_INTERFACES(BackendInterface)

    public:
        BackendSQLite();
        ~BackendSQLite();

        /**
        * openCollection does not use username, loginname or password. It opens
        * a sqlite db saved as a file on disk and is intended for single users.
        */
        bool openCollection(BackendOpenMode::BackendOpenModes openMode,
            const QString &location,
            const QString &username="", const QString &loginname="",
            const QString &password="");
        bool isCollectionOpen();
        void closeCollection();

        /**
         * SQLite is able to read incrementally. All read functions are
         * implemented.
         */
        bool canReadIncremental();
        bool readAllBillItems(QSet<BillItem> &billItems);
        bool startReadNextBillItem();
        bool hasMoreBillItems();
        bool readNextBillItem(BillItem &billItem);

        bool saveAllBillItems(QSet<BillItem> billItems);
        /**
         * Transactional saving is possible.
         */
        bool canSaveTransactional();
        /**
         * This cannot save incrementally.
         */
        bool canSaveAllIncremental();
        bool saveChange(Transaction transaction);
        /**
         * This cannot save incrementally.
         */
        bool saveBillItem(const BillItem &billItem);

        QString getLastErrorMessage();

        bool isRemote();
        QString getName();
        QString getDescription();
        QString getVersion();

        QString getDefaultFileExtenstion();

    private:
        bool execCreateEmptyTableQuery();
        bool execCreateTypeMarkerQuery();
        bool execDropTableQuery();
        bool execReadAllBillItemsQuery();
        bool execInsertBillItemQuery(const BillItem &billItem);
        bool execUpdateBillItemQuery(const BillItem &billItem);
        bool execDeleteBillItemQuery(const BillItem &billItem);
        bool execBillItemQuery(const QString &query);

        bool isDbVaild();
        bool createNewDb();
        bool isBillItemIdValid(const BillItem &billItem);

        QSqlDatabase m_db;
        QString m_dbName;
        QSqlQuery *m_query;
        /**
         * This is the only query we have to have to use outside of the
         * function associated with the query. hasMoreBillItems has to check
         * and see if this was the last query.
         * @see hasMoreBillItems
         */
        const QString m_readAllBillItemsQuery;
        QString m_errorMessage;
};

#endif /* BACKENDSQLITE_H */
